package ex.j2me;

import java.util.Vector;

public class LayerManager {

	private Vector layers;

	private int viewX, viewY, viewW, viewH;

	public LayerManager() {
		layers = new Vector();
		viewX = viewY = 0;
		viewW = viewH = Integer.MAX_VALUE;
	}

	public void append(Layer layer) {
		synchronized (this) {
			if (layer == null)
				throw new NullPointerException();
			layers.add(layer);
		}
	}

	public Layer getLayerAt(int i) {
		return (Layer) layers.get(i);
	}

	public int getSize() {
		return layers.size();
	}

	public void insert(Layer layer, int i) {
		synchronized (this) {
			if (layer == null)
				throw new NullPointerException();
			layers.insertElementAt(layer, i);
		}
	}

	public void remove(Layer layer) {
		synchronized (this) {
			if (layer == null)
				throw new NullPointerException();
			layers.remove(layer);
		}
	}

	public void setViewWindow(int x, int y, int width, int height) {
		synchronized (this) {
			if (width < 0 || height < 0)
				throw new IllegalArgumentException();
			viewX = x;
			viewY = y;
			viewW = width;
			viewH = height;
		}
	}

	public void paint(LGraphics g, int x, int y) {
		synchronized (this) {
			if (g == null) {
				throw new NullPointerException();
			}
			int clipX = g.getClipX();
			int clipY = g.getClipY();
			int clipW = g.getClipWidth();
			int clipH = g.getClipHeight();
			g.translate(x - viewX, y - viewY);
			g.clipRect(viewX, viewY, viewW, viewH);
			for (int i = getSize(); --i >= 0;) {
				Layer comp = getLayerAt(i);
				if (comp.isVisible()) {
					comp.paint(g);
				}
			}
			g.translate(-x + viewX, -y + viewY);
			g.setClip(clipX, clipY, clipW, clipH);
		}
	}

}
